﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace BenhVien
{
    public partial class fmEditStaff : Form
    {
        Connection cons = new Connection();
        private DataTable dt = new DataTable();
        private SqlDataAdapter da = new SqlDataAdapter();
        public fmEditStaff()
        {
            InitializeComponent();
            //binding();
        }

        private void btExit_Click(object sender, EventArgs e)
        {
            this.Close();
            frm_ManagerStaff _fmManagerStaff = new frm_ManagerStaff();
            _fmManagerStaff.Show();
        }
        private void binding()
        {
            tbMaNV.DataBindings.Clear();
            tbMaNV.DataBindings.Add("Text",dtGVStaff.DataSource,"MaNV");
            tbHo.DataBindings.Clear();
            tbHo.DataBindings.Add("Text", dtGVStaff.DataSource, "Ho");
            tbTen.DataBindings.Clear();
            tbTen.DataBindings.Add("Text", dtGVStaff.DataSource, "Ten");
            tbDiaChi.DataBindings.Clear();
            tbDiaChi.DataBindings.Add("Text", dtGVStaff.DataSource, "DiaChi");
            tbSdt.DataBindings.Clear();
            tbSdt.DataBindings.Add("Text", dtGVStaff.DataSource, "SoDienThoai");
            tbNgaySinh.DataBindings.Clear();
            tbNgaySinh.DataBindings.Add("Text", dtGVStaff.DataSource, "NgaySinh");
            cbGioiTinh.DataBindings.Clear();
            cbGioiTinh.DataBindings.Add("Text", dtGVStaff.DataSource, "GioiTinh");
            tbSCM.DataBindings.Clear();
            tbSCM.DataBindings.Add("Text", dtGVStaff.DataSource, "MaBaoHiem");
            cbChucVu.DataBindings.Clear();
            cbChucVu.DataBindings.Add("Text", dtGVStaff.DataSource, "Vitri");
            tbLuong.DataBindings.Clear();
            tbLuong.DataBindings.Add("Text", dtGVStaff.DataSource, "Luong");
            tbBacLuong.DataBindings.Clear();
            tbBacLuong.DataBindings.Add("Text", dtGVStaff.DataSource, "BacLuong");
            cbMaPhong.DataBindings.Clear();
            cbMaPhong.DataBindings.Add("Text", dtGVStaff.DataSource, "MaPhong");
            tbGioLam.DataBindings.Clear();
            tbGioLam.DataBindings.Add("Text", dtGVStaff.DataSource, "SoGioLamTrongTuan");
            cbHinhThuc.DataBindings.Clear();
            cbHinhThuc.DataBindings.Add("Text", dtGVStaff.DataSource, "HinhThucTraLuong");
            cbHopDong.DataBindings.Clear();
            cbHopDong.DataBindings.Add("Text", dtGVStaff.DataSource, "HopDong");
            
        }
       
        private void getdata()
        {
            SqlCommand command = new SqlCommand();
            command.Connection = cons.con;
            command.CommandType = CommandType.Text;
            command.CommandText = @"Select 
                                            NhanVien.MaNV as N'MaNV',
                                            Ho as N'Ho',
                                            Ten as N'Ten',
                                            DiaChi as N'DiaChi',
                                            SoDienThoai as N'SoDienThoai',
                                            NgaySinh as N'NgaySinh',
                                            (Case GioiTinh when 1 Then N'Nam' else N'Nữ' end) as N'GioiTinh',
                                            MaBaoHiem as N'MaBaoHiem',
                                            Vitri as N'Vitri',
                                            Luong as N'Luong',
                                            BacLuong as N'BacLuong',
                                            MaPhong as N'MaPhong',
                                            SoGioLamTrongTuan as N'SoGioLamTrongTuan',
                                            HinhThucTraLuong as N'HinhThucTraLuong',
                                            HopDong as N'HopDong'
                                            From NhanVien";
            da.SelectCommand = command;
            da.Fill(dt);
            dtGVStaff.DataSource = dt;
        }

        private void fmEditStaff_Load(object sender, EventArgs e)
        {
            this.cbChucVu.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cbGioiTinh.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cbHinhThuc.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cbHopDong.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cbMaPhong.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            cons.Connect();
           // displayRecord();
            getdata();
            binding();
        }

        private void btEdit_Click(object sender, EventArgs e)
        {
           // cons.Connect();
            DataRow row = dt.Select("MaNV = \'" +(tbMaNV.Text)+"\'")[0];
            row.BeginEdit();
            try
            {
                row["Ho"] = tbHo.Text;
                if (tbTen.Text != "")
                {
                    row["Ten"] = tbTen.Text;
                    row["DiaChi"] = tbDiaChi.Text;
                    row["SoDienThoai"] = tbSdt.Text;
                    try
                    {
                        row["NgaySinh"] = tbNgaySinh.Text;
                        if (cbGioiTinh.Text == "Nam") row["GioiTinh"] = 1;
                        else if (cbGioiTinh.Text == "Nữ") row["GioiTinh"] = 0;
                        if ((tbSCM.Text != null))
                        {
                            row["MaBaoHiem"] = tbSCM.Text;

                            if (!checkStaff(cbChucVu.Text)||checkVitri())
                            {
                                row["Vitri"] = cbChucVu.Text;
                                row["Luong"] = tbLuong.Text;
                                row["BacLuong"] = tbBacLuong.Text;
                                if (Convert.ToInt32(cbMaPhong.Text) > 0 && Convert.ToInt32(cbMaPhong.Text) < 18)
                                {
                                    row["MaPhong"] = cbMaPhong.Text;
                                    if (Convert.ToInt32(tbGioLam.Text) <= 56)
                                    {
                                        row["SoGioLamTrongTuan"] = tbGioLam.Text;
                                        row["HinhThucTraLuong"] = cbHinhThuc.Text;
                                        row["HopDong"] = cbHopDong.Text;
                                        row.EndEdit();
                                        SqlCommand commandUpdate = new SqlCommand();
                                        commandUpdate.Connection = cons.con;
                                        commandUpdate.CommandType = CommandType.Text;
                                        commandUpdate.CommandText = @"Update NhanVien Set Ho = @Ho,
                                                              Ten = @Ten,
                                                              DiaChi=@DiaChi,
                                                              SoDienThoai=@SoDienThoai,
                                                               NgaySinh=@NgaySinh, GioiTinh=@GioiTinh, MaBaoHiem=@MaBaoHiem, Vitri=@Vitri, Luong=@Luong,
                                                               BacLuong=@BacLuong, MaPhong=@MaPhong, SoGioLamTrongTuan=@SoGioLamTrongTuan,
                                                                HinhThucTraLuong=@HinhThucTraLuong, HopDong=@HopDong
                                                                Where MaNV=@MaNV";
                                        commandUpdate.Parameters.Add("@MaNV", SqlDbType.NVarChar, 50, "MaNV");
                                        commandUpdate.Parameters.Add("@Ho", SqlDbType.NChar, 10, "Ho");
                                        commandUpdate.Parameters.Add("@Ten", SqlDbType.NVarChar, 50, "Ten");
                                        commandUpdate.Parameters.Add("@DiaChi", SqlDbType.NVarChar, 50, "DiaChi");
                                        commandUpdate.Parameters.Add("@SoDienThoai", SqlDbType.NVarChar, 50, "SoDienThoai");
                                        commandUpdate.Parameters.Add("@NgaySinh", SqlDbType.Date, 15, "NgaySinh");
                                        commandUpdate.Parameters.Add("@GioiTinh", SqlDbType.Int, 50, "GioiTinh");
                                        commandUpdate.Parameters.Add("@MaBaoHiem", SqlDbType.NVarChar, 50, "MaBaoHiem");
                                        commandUpdate.Parameters.Add("@Vitri", SqlDbType.NVarChar, 50, "Vitri");
                                        commandUpdate.Parameters.Add("@Luong", SqlDbType.Float, 50, "Luong");
                                        commandUpdate.Parameters.Add("@BacLuong", SqlDbType.Float, 50, "BacLuong");
                                        commandUpdate.Parameters.Add("@MaPhong", SqlDbType.NVarChar, 50, "MaPhong");
                                        commandUpdate.Parameters.Add("@SoGioLamTrongTuan", SqlDbType.Int, 3, "SoGioLamTrongTuan");
                                        commandUpdate.Parameters.Add("@HinhThucTraLuong", SqlDbType.NVarChar, 50, "HinhThucTraLuong");
                                        commandUpdate.Parameters.Add("@HopDong", SqlDbType.NVarChar, 50, "HopDong");
                                        da.UpdateCommand = commandUpdate;
                                        da.Update(dt);
                                        dt.Clear();
                                        getdata();
                                        MessageBox.Show("Edit success!", "MesageBox", MessageBoxButtons.OK);
                                        //btExit_Click(null,null);
                                    }
                                    else
                                    {
                                        if (MessageBox.Show("Số giờ làm trong tuần không thể lớn hơn 56", "Giờ làm trong tuần", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                                        {
                                            this.Close();
                                            frm_ManagerStaff _fmManagerStaff = new frm_ManagerStaff();
                                            _fmManagerStaff.Show();
                                        }
                                    }
                                }
                                else
                                {
                                    if (MessageBox.Show("Bệnh viện chỉ có 17 phòng! Bạn có muốn tiếp tục?", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                                    {
                                        this.Close();
                                        frm_ManagerStaff _fmManagerStaff = new frm_ManagerStaff();
                                        _fmManagerStaff.Show();
                                    }
                                }
                            }
                            else
                            {
                                dt.Clear();
                                getdata();
                                if (MessageBox.Show("Giám đốc bênh viện hoặc giám đốc nhân viên chỉ có một người", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                                {
                                    this.Close();
                                    frm_ManagerStaff _fmManagerStaff = new frm_ManagerStaff();
                                    _fmManagerStaff.Show();
                                }
                            }
                            
                        }
                        else
                        {
                            
                        }
                        
                    }
                    catch(Exception)
                    {
                        if (MessageBox.Show("Ngày sinh sai! Bạn có muốn tiếp tục?", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                        {
                            this.Close();
                            frm_ManagerStaff _fmManagerStaff = new frm_ManagerStaff();
                            _fmManagerStaff.Show();
                        }
                    }
                    
                }
                else
                {
                    if (MessageBox.Show("Tên không được trống! Bạn có muốn tiếp tục?", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                    {
                        this.Close();
                        frm_ManagerStaff _fmManagerStaff = new frm_ManagerStaff();
                        _fmManagerStaff.Show();
                    }
                }
                
            }
            catch (Exception)
            {
                if (MessageBox.Show("Nội dung nhập đã sai. Bạn có muốn nhập lại?", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                {
                    this.Close();
                    frm_ManagerStaff _fmManagerStaff = new frm_ManagerStaff();
                    _fmManagerStaff.Show();
                }
            }
            
        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            if (dt.Rows.Count > 0)
            {
                DataTable dtdeleteUser = new DataTable();


                SqlCommand command = new SqlCommand();
                command.Connection = cons.con;

                command.CommandType = CommandType.Text;

                command.CommandText = "Select * From DangNhap Where MaNV = \'" + tbMaNV.Text + "\'";

                da.SelectCommand = command;
                da.Fill(dtdeleteUser);

                if (dtdeleteUser.Rows.Count > 0)
                {

                    DataRow row = dtdeleteUser.Select("MaNV = \'" + (tbMaNV.Text) + "\'")[0];
                    row.BeginEdit();
                    row.Delete();
                    row.EndEdit();
                    command.CommandText = "Delete From DangNhap Where DangNhap.MaNV=@MaNV";
                    command.Parameters.Add("@MaNV", SqlDbType.NVarChar, 50, "MaNV");
                    da.DeleteCommand = command;
                    da.Update(dtdeleteUser);

                    //MessageBox.Show("Không có dữ liệu để xóa!", "Xóa", MessageBoxButtons.OK);
                    DataRow rows = dt.Select("MaNV = \'" + (tbMaNV.Text) + "\'")[0];
                    rows.BeginEdit();
                    rows.Delete();
                    rows.EndEdit();
                    command.CommandText = "Delete From NhanVien Where NhanVien.MaNV=@MaNV";
                    // command.Parameters.Add("@MaNV", SqlDbType.NVarChar, 50, "MaNV");
                    da.DeleteCommand = command;
                    da.Update(dt);
                    MessageBox.Show("Delete success!", "MesageBox", MessageBoxButtons.OK);
                }
                else
                {
                    DataRow rows = dt.Select("MaNV = \'" + (tbMaNV.Text) + "\'")[0];
                    rows.BeginEdit();
                    rows.Delete();
                    rows.EndEdit();
                    command.CommandText = "Delete From NhanVien Where NhanVien.MaNV=@MaNV";
                    command.Parameters.Add("@MaNV", SqlDbType.NVarChar, 50, "MaNV");
                    da.DeleteCommand = command;
                    da.Update(dt);
                    MessageBox.Show("Delete success!", "MesageBox", MessageBoxButtons.OK);
                }


            }
            else
            {
                MessageBox.Show("Không có dữ liệu để xóa!", "Xóa", MessageBoxButtons.OK);
            }
            
            
        }
        private bool checkStaff(String cv)
        {
            Boolean checkstaff = false;

            DataTable dtcheckGDBV = new DataTable();
            DataTable dtcheckGDNV = new DataTable();
            SqlCommand commandcheckstaff = new SqlCommand();
            commandcheckstaff.Connection = cons.con;
            commandcheckstaff.CommandType = CommandType.Text;
            commandcheckstaff.CommandText = "Select Vitri from NhanVien Where Vitri= 'GD BV'";
            da.SelectCommand = commandcheckstaff;
            da.Fill(dtcheckGDBV);

            commandcheckstaff.CommandText = "Select Vitri from NhanVien Where Vitri= 'GD NV'";
            da.SelectCommand = commandcheckstaff;
            da.Fill(dtcheckGDNV);
            if (dtcheckGDBV.Rows.Count > 0 && cv == "GD BV") checkstaff = true;
            if (dtcheckGDNV.Rows.Count > 0 && cv == "GD NV") checkstaff = true;

            return checkstaff;
        }
        private bool checkVitri()
        {
            DataTable dtVitri = new DataTable();
            Boolean check = false;
            SqlCommand command = new SqlCommand();
            command.Connection = cons.con;

            command.CommandType = CommandType.Text;

            command.CommandText = "Select Vitri From NhanVien Where MaNV = \'" + tbMaNV.Text + "\'";

            da.SelectCommand = command;
            // MessageBox.Show("loi", "MessageBox", MessageBoxButtons.OK);
            da.Fill(dtVitri);
            if (dtVitri.Rows.Count > 0)
                if ((dtVitri.Rows[0][0].ToString() == "GD BV")&&cbChucVu.Text=="GD BV" || (dtVitri.Rows[0][0].ToString() == "GD NV")&&cbChucVu.Text=="GD NV") check = true;

            return check;
        }

    }
}
